package cn.vw.hi.user.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.vw.hi.commen.tools.Helper;
import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.user.manager.UserManager;
import cn.vw.hi.user.model.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private UserManager userManager=null;
	private User user=null;
	private int page;
	private int rows;
	private String urlback=null;
	private String ajax=null;
	
	@Override
	public String execute() throws Exception {
		return "success";
	}
	//添加用户
	public String addUser()throws Exception{
		user.setUsername(Helper.decode(user.getUsername()));
		JSONObject json=new JSONObject();
		User userFind=userManager.findUserByEmail(user);
		if(userFind!=null)
		{
			json.put("info", "have");
		}else
		{
			boolean isSuccess=userManager.addUser(user);
			json.put("info", isSuccess);
		}
		backText(json.toString());
		return "Data";
	}
	
	//用户登录
	public String loginUser()throws Exception{
		user=userManager.loginUser(user);
		this.clearMessages();
		if(user!=null)
		{
			ActionContext.getContext().getSession().put("user",user);
			if(user.getLevel()==1)
			{
				urlback="/admin/adminindex.jsp";
			}else
			{
				urlback="/default.jsp";
			}
		}else
		{
			this.addActionMessage("用户名或密码错误！");
			urlback="/user/login.jsp";
		}
		if(ajax!=null&&ajax.equals("ajax"))
		{
			return "Data";
		}else
		{
			return "Login";
		}
	}
	//用户退出
	public String loginOut() throws Exception{
		 ActionContext.getContext().getSession().remove("user");
		return "LoginOut";
	}
	//查找用户 
	public String findUser()throws Exception{
		boolean isLogin=checkUserSession();
		JSONObject json=new JSONObject();
		if(isLogin)
		{
			boolean isSuccess=userManager.findUser(user);
			json.put("info", isSuccess);
		}else
		{
			json.put("info", "nologin");
		}
		backText(json.toString());
		return "Data";
	}
	//删除用户
	public String deleteUser() throws Exception
	{
		boolean isSuccess=userManager.deleteUser(user);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//修改用户
	public String modifyUser() throws Exception{
		User sessionUser=(User)ActionContext.getContext().getSession().get("user");
		JSONObject json=new JSONObject();
		
		if(!user.getPassword().split(",")[1].equals(sessionUser.getPassword()))
		{
			json.put("info", "OldERROR");
		}else
		{
			user.setPassword(user.getPassword().split(",")[0]);
		    boolean isSuccess=userManager.modifyUser(user);
		    if(isSuccess)
		    {
		    	json.put("info", isSuccess);
		    	sessionUser.setPassword(user.getPassword().split(",")[0]);
		    	ActionContext.getContext().getSession().put("user", sessionUser);
		    }else
		    {
		    	json.put("info", isSuccess);
		    }
		}
		backText(json.toString());
		return "Data";
	}
	//查检用户是否登录
	public boolean checkUserSession() 
	{
		User user=(User)ActionContext.getContext().getSession().get("user");
		if(user==null)
		{
			return false;
		}
		return true;
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	//用户列表
	public String userList() throws Exception
	{
		Page pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(rows);
		List userList=userManager.userList(pageObj);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<userList.size();i++)
		{
			User  user=(User)userList.get(i);
			JSONObject jsontemp = new JSONObject();
			jsontemp.put("id",user.getId());
			jsontemp.put("email", user.getEmail());
			jsontemp.put("username", user.getUsername());
			jsontemp.put("password",user.getPassword());
			jsontemp.put("delete", "<a href='javascript:deleteUser("+user.getId()+")'>删除</>");
			jsontemp.put("modify", "<a href='website!modifyColumnPage.action?websiteColumn.id="+user.getId()+"'>修改</>");
			jsonArray.add(jsontemp);
		}
		json.put("total",pageObj.getRowCount());
		json.put("rows",jsonArray);
		backText(json.toString());
		return "Data";
	}
	public UserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getUrlback() {
		return urlback;
	}
	public void setUrlback(String urlback) {
		this.urlback = urlback;
	}
	public void setAjax(String ajax) {
		this.ajax = ajax;
	}

}
